<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of Report
 *
 * @author axioo
 */
namespace Controllers;
use Resources,
    Libraries;
class Report extends Libraries\PController {
    public function __construct() {
        parent::__construct();
        $this->request = new Resources\Request;
        $this->session = new Resources\Session;
        $this->db = new Libraries\Mysql;
    }
    public function test() {
//        var_dump(Libraries\CDate::x_week_range('2013-07-13'));
//        var_dump(Libraries\CDate::get_date_diff_in_day('2013-07-01', '2013-07-05'));
        var_dump(Libraries\CDate::create_week_period('2013-02-17', '2013-03-30'));
    }
    protected function footer($objPHPExcel, $start, $file_name, $format,
            $html_title) {
        $start++;
        $user_id = Libraries\u::get_user_id($this->session);
        $user = $this->db->row(Libraries\CDb::get_user_by_id($user_id));
        $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:G$start")->
                setCellValue("A$start", "Dicetak oleh: " . $user->username);
        $start++;
        $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:G$start")->
                setCellValue("A$start",
                        "Pada tanggal " . Libraries\CDate::Now('d/m/Y') . " jam " .
                        Libraries\CDate::get_time_now());
        ob_end_clean();
        ob_start();
        if ($format == 'excel') {
            header('Content-Type: application/vnd.ms-excel');
            header("Content-Disposition: attachment;filename=$file_name.xls");
            header('Cache-Control: max-age=0');
            $objWriter = //PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                    Resources\Import::vendor('PHPExcel/PHPExcel_IOFactory',
                            'PHPExcel_IOFactory::createWriter',
                            array($objPHPExcel, 'Excel5'));
            $objWriter->save('php://output');
        } else {
            $objPHPExcel->getActiveSheet()->setShowGridlines(false);
            $objWriter = Resources\Import::vendor('PHPExcel/PHPExcel_Writer_HTML',
                            'PHPExcel_Writer_HTML', array($objPHPExcel));
            $header = $objWriter->generateHTMLHeader(true);
            $header = str_replace("<body>", "<body onload='window.print();'>",
                    $header);
            $header = str_replace("Untitled Spreadsheet", $html_title, $header);
            $html = $header . $objWriter->generateStyles(true) . $objWriter->
                            generateSheetData() . $objWriter->generateHTMLFooter();
            if ($format == 'pdf') {
                $mPDF1 = Resources\Import::vendor('mpdf/mpdf', 'mPDF',
                                array('', 'A4'));
                $mPDF1->WriteHTML($html);
                $mPDF1->Output($file_name . 'pdf', 'D');
            } else {
                echo $html;
            }
        }
    }
    protected function header(&$objPHPExcel, &$start, $worksheet_name,
            $report_title) {
        $objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
        $papersize = Resources\Import::vendor('PHPExcel/PHPExcel_Worksheet_PageSetup',
                        'PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4');
        $objPHPExcel->setActiveSheetIndex(0)->getPageSetup()
                ->setPaperSize($papersize);
        $objPHPExcel->setActiveSheetIndex(0)->getPageMargins()->setLeft(0.1 / 2.54);
        $objPHPExcel->setActiveSheetIndex(0)->getPageMargins()->setRight(0.1 / 2.54);
        $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:G$start")->
                setCellValue("A$start", $report_title)->getStyle("A$start")->getFont()->setSize(18)->
                setBold(true);
        $start++;
        $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:G$start")->
                setCellValue("A$start", "HINENI")->getStyle("A$start")->getFont()->setSize(14)->
                setBold(true);
        $start++;
        $objPHPExcel->getActiveSheet()->setTitle($worksheet_name);
    }
    public function PengeluaranPerKavling() {
        if (!Libraries\u::isAjaxRequest()) {
            if (isset($_POST) && !empty($_POST)) {
                $format = $this->get_post('format');
//                $start_date = $this->get_post('trans_date_mulai');
                $end_date = $this->get_post('trans_date_sampai');
                $id_kavling = $this->get_post('id_kavling');
                $kavling = $this->db->row(Libraries\CDb::get_kavling_by_id($id_kavling));
                $nama_kavling = $kavling->nama;
                $proyek = $this->db->row(\Libraries\CDb::get_proyek_by_id($kavling->id_proyek));
                $start_date = is_null($proyek->tgl_mulai) ? null : $proyek->tgl_mulai;
                $end_date = is_null($proyek->tgl_selesai) ? \Libraries\CDate::Now('Y-m-d')
                            : $proyek->tgl_selesai;
                $start = 1;
                $file_name = 'PengeluaranPerKavling';
                $worksheet_name = 'Pengeluaran per Kavling';
                $objPHPExcel = Resources\Import::vendor('PHPExcel/PHPExcel');
                $this->header($objPHPExcel, $start, $worksheet_name,
                        "PENGELUARAN PER KAVLING");
                $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:F$start")->
                        setCellValue("A$start", "Kavling : " . $nama_kavling)->getStyle("A$start")->getFont()->
                        setSize(12)->setBold(true);
                $start++;
                $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:F$start")->
                        setCellValue("A$start",
                                "Dari tanggal " . Libraries\CDate::sql2date($start_date,
                                        'd F Y') .
                                " sampai tanggal " . Libraries\CDate::sql2date($end_date,
                                        'd F Y'))->getStyle("A$start")->getFont()->
                        setSize(12)->setBold(true);
                $objPHPExcel->getActiveSheet()->setTitle($worksheet_name);
                $start++;
                $start++;
                $start_body = $start;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A$start",
                                "Rekap Minggu")->setCellValue("B$start",
                                "Tanggal")->setCellValue("C$start", "Tenaga")->setCellValue("D$start",
                                "Bahan")->setCellValue("E$start", "Jumlah")->getStyle("A$start:E$start")
                        ->getFont()->setBold(true);
                $start++;

                $mingguan = Libraries\CDate::create_week_period($proyek->tgl_mulai,
                                $proyek->tgl_selesai);
                $total_tenaga = $total_bahan = $total_all = 0;
                foreach ($mingguan as $key => $val) {
                    $tenaga = 0;
                    $bahan = $this->db->getVar(Libraries\CDb::get_lap_trans_bahan($val['start'],
                                    $val['end'], $id_kavling));
                    $total_tenaga += $tenaga;
                    $total_bahan += $bahan;
                    $bahan = is_null($bahan) ? 0 : $bahan;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A$start",
                                    "Minggu " . ($key + 1))->
                            setCellValue("B$start",
                                    Libraries\CDate::sql2date($val['start'],
                                            "d M Y") .
                                    " - " . Libraries\CDate::sql2date($val['end'],
                                            "d M Y"))
                            ->setCellValue("C$start",
                                    $format == 'excel' ? $tenaga : number_format($tenaga))
                            ->setCellValue("D$start",
                                    $format == 'excel' ? $bahan : number_format($bahan))
                            ->setCellValue("E$start",
                                    $format == 'excel' ? $tenaga + $bahan : number_format($tenaga
                                                    + $bahan));
                    $start++;
                }
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A$start",
                                'Total')->
                        setCellValue("C$start",
                                $format == 'excel' ? $total_tenaga : number_format($total_tenaga))->
                        setCellValue("D$start",
                                $format == 'excel' ? $total_bahan : number_format($total_bahan))->
                        setCellValue("E$start",
                                $format == 'excel' ? $total_tenaga + $total_bahan
                                            : number_format($total_tenaga + $total_bahan));
                $start++;
                $end_body = $start - 1;
                $border_style = Resources\Import::vendor('PHPExcel/PHPExcel_Style_Border',
                                'PHPExcel_Style_Border::BORDER_THIN');
                $styleArray = array('borders' => array('allborders' => array('style' =>
                            $border_style)));
                $objPHPExcel->setActiveSheetIndex(0)->getStyle("A$start_body:E$end_body")->
                        applyFromArray($styleArray);
                $start_row = $start_body + 1;
                $format_style = 0;
                if ($format == 'excel') {
                    $border_style = Resources\Import::vendor('PHPExcel/PHPExcel_Style_NumberFormat',
                                    'PHPExcel_Style_NumberFormat::FORMAT_ACCOUNTING');
                    $objPHPExcel->setActiveSheetIndex(0)->getStyle("C$start_row:E$end_body")->
                            getNumberFormat()->setFormatCode($border_style);
                } else {
                    $border_style = Resources\Import::vendor('PHPExcel/PHPExcel_Style_Alignment',
                                    'PHPExcel_Style_Alignment::HORIZONTAL_RIGHT');
                    $objPHPExcel->setActiveSheetIndex(0)->getStyle("C$start_body:E$end_body")->
                            getAlignment()->setHorizontal($border_style);
                }
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("A")->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("B")->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("C")->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("D")->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("E")->setAutoSize(true);
                $this->footer($objPHPExcel, $start, $file_name, $format,
                        $worksheet_name);
            } else {
                $this->redirect('home');
            }
        }
    }
    
    public function PengeluaranPerProyek(){
        if (!Libraries\u::isAjaxRequest()) {
            if (isset($_POST) && !empty($_POST)) {
                $format = $this->get_post('format');
//                $start_date = $this->get_post('trans_date_mulai');
                $end_date = $this->get_post('trans_date_sampai');
                $id_proyek = $this->get_post('id_proyek');
                $kavling = $this->db->results(Libraries\CDb::get_kavling_by_proyek_id($id_proyek),'array');
//                $nama_kavling = $kavling->nama;
                $proyek = $this->db->row(\Libraries\CDb::get_proyek_by_id($id_proyek));                
                $start_date = is_null($proyek->tgl_mulai) ? null : $proyek->tgl_mulai;
                $end_date = is_null($proyek->tgl_selesai) ? \Libraries\CDate::Now('Y-m-d')
                            : $proyek->tgl_selesai;
                $start = 1;
                $file_name = 'PengeluaranPerProyek';
                $worksheet_name = 'Pengeluaran per Proyek';
                $objPHPExcel = Resources\Import::vendor('PHPExcel/PHPExcel');
                $this->header($objPHPExcel, $start, $worksheet_name,
                        "PENGELUARAN PER PROYEK");
                $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:F$start")->
                        setCellValue("A$start", "Proyek : " . $proyek->nama)->getStyle("A$start")->getFont()->
                        setSize(12)->setBold(true);
                $start++;
                $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$start:F$start")->
                        setCellValue("A$start",
                                "Dari tanggal " . Libraries\CDate::sql2date($start_date,
                                        'd F Y') .
                                " sampai tanggal " . Libraries\CDate::sql2date($end_date,
                                        'd F Y'))->getStyle("A$start")->getFont()->
                        setSize(12)->setBold(true);
                $objPHPExcel->getActiveSheet()->setTitle($worksheet_name);
                $start++;
                $start++;
                $start_body = $start;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A$start",
                                "Nama Kavling")->setCellValue("B$start", "Tenaga")->setCellValue("C$start",
                                "Bahan")->setCellValue("D$start", "Jumlah")->getStyle("A$start:D$start")
                        ->getFont()->setBold(true);
                $start++;
//                $mingguan = Libraries\CDate::create_week_period($proyek->tgl_mulai,
//                                $proyek->tgl_selesai);
                $total_tenaga = $total_bahan = $total_all = 0;
                foreach ($kavling as $key => $val) {
                    $tenaga = 0;
                    $bahan = $this->db->getVar(Libraries\CDb::get_lap_trans_bahan($start_date,
                                    $end_date, $val['id_kavling']));
                    $total_tenaga += $tenaga;
                    $total_bahan += $bahan;
                    $bahan = is_null($bahan) ? 0 : $bahan;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A$start",
                                    $val['nama'])
                            ->setCellValue("B$start",
                                    $format == 'excel' ? $tenaga : number_format($tenaga))
                            ->setCellValue("C$start",
                                    $format == 'excel' ? $bahan : number_format($bahan))
                            ->setCellValue("D$start",
                                    $format == 'excel' ? $tenaga + $bahan : number_format($tenaga
                                                    + $bahan));
                    $start++;
                }
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A$start",
                                'Total')->
                        setCellValue("B$start",
                                $format == 'excel' ? $total_tenaga : number_format($total_tenaga))->
                        setCellValue("C$start",
                                $format == 'excel' ? $total_bahan : number_format($total_bahan))->
                        setCellValue("D$start",
                                $format == 'excel' ? $total_tenaga + $total_bahan
                                            : number_format($total_tenaga + $total_bahan));
                $start++;
                $end_body = $start - 1;
                $border_style = Resources\Import::vendor('PHPExcel/PHPExcel_Style_Border',
                                'PHPExcel_Style_Border::BORDER_THIN');
                $styleArray = array('borders' => array('allborders' => array('style' =>
                            $border_style)));
                $objPHPExcel->setActiveSheetIndex(0)->getStyle("A$start_body:D$end_body")->
                        applyFromArray($styleArray);
                $start_row = $start_body + 1;
                $format_style = 0;
                if ($format == 'excel') {
                    $border_style = Resources\Import::vendor('PHPExcel/PHPExcel_Style_NumberFormat',
                                    'PHPExcel_Style_NumberFormat::FORMAT_ACCOUNTING');
                    $objPHPExcel->setActiveSheetIndex(0)->getStyle("B$start_row:D$end_body")->
                            getNumberFormat()->setFormatCode($border_style);
                } else {
                    $border_style = Resources\Import::vendor('PHPExcel/PHPExcel_Style_Alignment',
                                    'PHPExcel_Style_Alignment::HORIZONTAL_RIGHT');
                    $objPHPExcel->setActiveSheetIndex(0)->getStyle("B$start_body:D$end_body")->
                            getAlignment()->setHorizontal($border_style);
                }
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("A")->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("B")->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("C")->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("D")->setAutoSize(true);
//                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension("E")->setAutoSize(true);
                $this->footer($objPHPExcel, $start, $file_name, $format,
                        $worksheet_name);
            } else {
                $this->redirect('home');
            }
        }
    }
}
?>
